<?PHP
session_start();
include("../config/connect.php");
include("../public/common.php");

if( !isset($_SESSION["gs_user"]) ) 
{
	$url = "Location:login.php";
	header($url);
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>购物兔</title>
<style type="text/css">
<!--
body,td,th {
	font-family: 宋体;
	font-size: 13px;
	color: #000;
}
body {
	background-color: #FFF;
}
-->
</style>
<script language="javascript">

</script>
</head>

<body>
<!--<a href="add_trade.php?opt=add">添加记录</a>-->
<table cellpadding="0" cellspacing="0" border="1" align="center">
	<tr>
    	<td height="25" width="250" align="center">支付宝账号</td>
        <td width="141" align="center">手机号码</td>
        <td width="121" align="center">共推广用户数</td>
      	<td width="198" align="center">推广成功奖励</td>
        <td width="170" 	align="center">推广提成(元)</td>
    </tr>
<?php

function GetRecommendSuccessCnt( $id, &$nAll )
{
	$sql_sub = "select tb_id from user where commend_id='$id'";
	$result = mysql_query( $sql_sub );
	$SucCnt = 0;
	$nAll = 0;
	$price = 0;
	while( $row = mysql_fetch_assoc( $result ))
	{
		$tb_id = $row["tb_id"];
		
		$price = GetcommendPrice( $tb_id );
		if( $price != 0 )
			$SucCnt ++;
			
		$nAll += $price;
	}
	
	return $SucCnt;
}

function GetcommendPrice( $tb_id )
{
	$price = 0;
	$sql = "select all_return from return_record where tb_id='$tb_id'";
	$result = mysql_query( $sql );
	while( $row = mysql_fetch_assoc( $result ))
	{
		$price += $row["all_return"];
	}
	
	return $price;
}

function GetAllRecommendCnt( $id )
{
	$sql_sub = "select commend_id from user where commend_id='$id'";
	$result = mysql_query( $sql_sub );
	$num_rows = mysql_num_rows($result);
	return $num_rows;
}

$sql = "select * from user order by id";
$result = mysql_query( $sql );
while( $result && $row = mysql_fetch_assoc( $result ))
{
	$id = $row["id"];
	$num_rows = GetAllRecommendCnt( $id );
	if( $num_rows == 0 )
		continue;
		
	$nAll = 0;
	$Cnt = GetRecommendSuccessCnt( $id, &$nAll );
	
?>
<tr >
    <td height="25"  align="center"><?PHP echo $row["tb_id"]; ?></td>
     <td height="25"  align="center"><?PHP echo $row["phone"]; ?>&nbsp;</td>
    <td align="center" title=""><?PHP echo $num_rows; ?></td>
	<td align="center"><?PHP echo $Cnt; ?>&nbsp;</td>
	<td align="center"><?PHP echo $nAll*0.01; ?>&nbsp;</td>
	
<?PHP
}
?>
</table>
</body>
</html>
